/**
 * Copyright (c) 2018 湖南牛数 All rights reserved.
 * <p>
 * https://www.dshubs.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.dshubs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.ConvertUtils;
import io.dshubs.dao.SysDictDataDao;
import io.dshubs.dao.SysDictTypeDao;
import io.dshubs.dto.SysDictDataDTO;
import io.dshubs.dto.SysDictTypeDTO;
import io.dshubs.entity.DictData;
import io.dshubs.entity.DictType;
import io.dshubs.entity.SysDictDataEntity;
import io.dshubs.entity.SysDictTypeEntity;
import io.dshubs.service.SysDictTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 字典类型
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeDao, SysDictTypeEntity> implements SysDictTypeService {
    @Autowired
    private SysDictDataDao sysDictDataDao;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @Override
    public PageData<SysDictTypeDTO> page(Map<String, Object> params) {
        IPage<SysDictTypeEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                getWrapper(params)
        );

        return getPageData(page, SysDictTypeDTO.class);
    }

    private QueryWrapper<SysDictTypeEntity> getWrapper(Map<String, Object> params) {
        String dictType = (String) params.get("dictType");
        String dictName = (String) params.get("dictName");
        Long tenantCode = SecurityUser.getUser().getTenantCode();
        QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dictType), "dict_type", dictType);
        wrapper.like(StringUtils.isNotBlank(dictName), "dict_name", dictName);
        wrapper.eq(tenantCode != null, "tenant_code", tenantCode);
        return wrapper;
    }

    @Override
    public SysDictTypeDTO get(Long id) {
        SysDictTypeEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SysDictTypeDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictTypeDTO dto) {
        SysDictTypeEntity entity = ConvertUtils.sourceToTarget(dto, SysDictTypeEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictTypeDTO dto) {
        SysDictTypeEntity entity = ConvertUtils.sourceToTarget(dto, SysDictTypeEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除
        deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<DictType> getAllList() {
        List<DictType> typeList = baseDao.getDictTypeList();
        List<DictData> dataList = sysDictDataDao.getDictDataList();
        for (DictType type : typeList) {
            for (DictData data : dataList) {
                if (type.getId().equals(data.getDictTypeId())) {
                    type.getDataList().add(data);
                }
            }
        }
        return typeList;
    }

    @Override
    public List<DictType> getDictTypeList() {
        return baseDao.getDictTypeList();
    }

    @Override
    public List<SysDictDataDTO> getDictList(Long code) {
        List<SysDictDataEntity> dataEntities = new ArrayList<>();
        List<SysDictTypeEntity> entities = this.baseDao.selectList(new QueryWrapper<SysDictTypeEntity>()
                .eq("dict_type", "BASE_ZDLX").eq("dict_name", code));

        if(CollectionUtils.isNotEmpty(entities)) {
            dataEntities = sysDictDataDao.selectList(new QueryWrapper<SysDictDataEntity>()
                    .eq("dict_type_id", entities.get(0).getId())
                    .eq("dict_label", "FIELD")
            );

        }

        return ConvertUtils.sourceToTarget(dataEntities, SysDictDataDTO.class);
    }
}